﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Contract_ContractWarn : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Auth();
            PaySetPager();
            RenewSetPager();
        }
    }

    protected void Pay_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Pay"))
        {
            string sqlStr = "UPDATE U_CELL_CONTRACT_T SET Pay_Date = add_months(Pay_Date,12) WHERE Contract_No =:Contract_No";
            OracleParameter[] _params = {
                new OracleParameter(":Contract_No",OracleType.VarChar,64)
            };
            _params[0].Value = e.CommandArgument.ToString();
            int res = OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
            if (res == 1)
            {
                PaySetPager();
                JQueryAlert("合同" + e.CommandArgument.ToString() + "付款成功!");
            }
        }
    }

    protected void PayPager_PageChanged(object sender, EventArgs e)
    {
        PaySetPager();
    }

    protected void Renew_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Renew"))
        {
            Response.Redirect("~/Contract/CellContract.aspx?Renew=" + e.CommandArgument.ToString());
        }
    }

    protected void RenewPager_PageChanged(object sender, EventArgs e)
    {
        RenewSetPager();
    }

    private void Auth()
    {
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            Company.Text = dr["company"].ToString();
        }
    }

    private void PaySetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetPayList();
        PayPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = PayPager.PageSize;
        pds.CurrentPageIndex = PayPager.CurrentPageIndex - 1;
        PayList.DataSource = pds;
        PayList.DataBind();
    }

    private DataSet GetPayList()
    {
        string sqlStr = @"select Contract_No,Cell_Chname,Cell_Type,Other_Name
                                   ,TO_CHAR(Begin_Date,'FMyyyy-mm-dd') AS Begin_Date
                                   ,TO_CHAR(End_Date,'FMyyyy-mm-dd') AS End_Date,Contract_Value
                                   ,TO_CHAR(Pay_Date,'FMyyyy-mm-dd') AS Pay_Date,Status
                              from U_CELL_CONTRACT_T 
                             WHERE STATUS = '正常' 
                               AND Pay_Date < to_date(to_char(last_day(add_months(sysdate,-9))+1,'yyyy-MM-dd'))
                               AND Cell_Chname LIKE '" + Company.Text + "%' ORDER BY Pay_Date";
        return OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null);
    }

    private void RenewSetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetRenewList();
        RenewPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = RenewPager.PageSize;
        pds.CurrentPageIndex = RenewPager.CurrentPageIndex - 1;
        RenewList.DataSource = pds;
        RenewList.DataBind();
    }

    private DataSet GetRenewList()
    {
        string sqlStr = @"select Contract_No,Cell_Chname,Cell_Type,Other_Name
                                   ,TO_CHAR(Begin_Date,'FMyyyy-mm-dd') AS Begin_Date
                                   ,TO_CHAR(End_Date,'FMyyyy-mm-dd') AS End_Date,Contract_Value
                                   ,TO_CHAR(Pay_Date,'FMyyyy-mm-dd') AS Pay_Date,Status
                              from U_CELL_CONTRACT_T 
                             WHERE STATUS = '正常' 
                               AND END_DATE < to_date(to_char(last_day(add_months(sysdate,5))+1,'yyyy-MM-dd'))
                               AND Cell_Chname LIKE '" + Company.Text + "%' ORDER BY End_Date";
        return OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null);
    }
}
